package com.dq.controller.user;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dq.context.Constant;
import com.dq.domain.sys.user.user.BossUser;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.CustomerQuery;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.AbstractEntity;
import com.dq.domain.position.OrgPosition;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author luchangtong
 * @Date 2019/12/26 10:02
 */
@RestController
@RequestMapping("/sys/boss/staffUser")
@Api(description = "员工管理")
public class BossStaffUserController extends AllService {

    @ApiOperation("员工列表")
    @PostMapping("list")
    public Vo list(@RequestBody CustomerQuery customerQuery)throws AdminException {
        List<BossUser> staffUserList = staffUserService.findByWhere(customerQuery);
        Long totalCount = staffUserService.countByWhere(customerQuery);
        PageHelper.startPage(customerQuery.getPage(),customerQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<StaffUser.Vo>) VO.toVOCollection(staffUserList));
        pageInfo.setTotal(totalCount);
        return new Vo(pageInfo);
    }

    @ApiOperation("员工账号 冻结/启用")
    @PostMapping("frozen/{id}")
    public Vo frozen(@PathVariable Long id)throws AdminException{
        StaffUser staffUser = dao.get(StaffUser.class,id);
        StringBuffer stringBuffer = new StringBuffer();
        if(StaffUser.Status.off.name.equals(staffUser.getStatus().name)){
            staffUser.setStatus(StaffUser.Status.on);
            staffUser.setLastModifyTime(System.currentTimeMillis());
            stringBuffer.append("已启用");
        }else {
            staffUser.setStatus(StaffUser.Status.off);
            staffUser.setLastModifyTime(System.currentTimeMillis());
            stringBuffer.append("已冻结");
        }
        StaffUser save = staffUserRepository.save(staffUser);

        return new Vo(save.toVO());
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("员工账号删除")
    public Vo delete(@PathVariable Long id)throws AdminException{
        StaffUser staffUser = dao.get(StaffUser.class,id);
        if(staffUser == null){
            throw new AdminException("请重新选择");
        }
        staffUser.setDeleted(true);
        staffUserRepository.save(staffUser);
        return new Vo(HttpStatus.OK);
    }

    @PostMapping("/staffQuit/{id}")
    @ApiOperation("在/离职")
    public Vo staffQuit(@PathVariable Long id)throws AdminException{
        StaffUser staffUser = dao.get(StaffUser.class,id);
        if(staffUser == null){
            throw new AdminException("请重新选择员工");
        }

        staffUserRepository.save(staffUser);
        return new Vo(HttpStatus.OK);
    }

    @PostMapping("/add")
    @ApiOperation("员工账号添加")
    public Vo add(@RequestBody StaffUser staffUserAdd)throws AdminException{
        SysUser sysUser = shiroService.getUser();
        staffUserAdd.setCardNo(AbstractEntity.generyDatetimeAndRandomNum(Constant.ORDERS_SUFF_COUNT));
        staffUserAdd.setPassword(sysUserService.getPassword(staffUserAdd.getPassword()));
        staffUserAdd.setSection(sysUser.getSection());
        staffUserAdd.setStatus(SysUser.Status.on);
        staffUserAdd.setCreateUser(sysUser);
        staffUserRepository.save(staffUserAdd);
        return new Vo(HttpStatus.OK);
    }
    @PostMapping("/addStaff/{positionId}")
    @ApiOperation("员工档案添加")
    public Vo add(@PathVariable Long positionId,@RequestBody StaffUser staffUserAdd)throws AdminException{
        OrgPosition orgPosition = dao.get(OrgPosition.class,positionId);
        if(null==orgPosition){throw new AdminException("岗位信息不存在");}
        staffUserAdd.setOrgPosition(orgPosition);

        staffUserService.save(staffUserAdd);

        return new Vo(HttpStatus.OK);
    }

    @PostMapping("/edit/{userId}")
    @ApiOperation("修改员工账号")
    public Vo edit(@PathVariable Long userId,@RequestBody StaffUser staffUserEdit)throws AdminException{
        StaffUser staffUser = dao.get(StaffUser.class,userId);
        if(null==staffUser){throw new AdminException("员工信息不存在");}
        BeanUtils.copyProperties(staffUserEdit,staffUser, BeanNullUtil.getNullPropertyNames(staffUserEdit));

        if(StringUtils.isNotEmpty(staffUserEdit.getPassword())){
            staffUser.setPassword(sysUserService.getPassword(staffUserEdit.getPassword()));
        }

        // 员工账号 判空
        staffUserService.judgeAcObject(staffUser, staffUser.getId());

        staffUserRepository.save(staffUser);
        return new Vo(HttpStatus.OK);
    }
    @PostMapping("/editStaff/{userId}/{positionId}")
    @ApiOperation("修改员工档案")
    public Vo edit(@PathVariable Long userId,@PathVariable Long positionId,@RequestBody StaffUser staffUserEdit)throws AdminException{
        StaffUser staffUser = dao.get(StaffUser.class,userId);
        if(null==staffUser){throw new AdminException("员工信息不存在");}

        OrgPosition orgPosition = dao.get(OrgPosition.class, positionId);
        if(null==orgPosition){throw new AdminException("岗位信息不存在");}
        staffUser.setOrgPosition(orgPosition);

        BeanUtils.copyProperties(staffUserEdit,staffUser, BeanNullUtil.getNullPropertyNames(staffUserEdit));

        // 员工档案 判空
        staffUserService.judgeObject(staffUser, staffUser.getId());

        staffUserRepository.save(staffUser);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("员工下拉框选择列表")
    @GetMapping("staffUserSelectList")
    public Vo listStaffUser() throws AdminException{
        List<BossUser> bossUsers = bossUserRepository.findByDeletedFalse();
        JSONArray array = new JSONArray();
        bossUsers.forEach(bossUser -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", bossUser.getId());
            jsonObject.put("name", bossUser.getUserRealcName());
            array.add(jsonObject);
        });
        return new ListDataVo(array);
    }
}
